﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using FW;
using System.IO;
using kserv;


namespace kcsrv
{
    public partial class Service1 : ServiceBase
    {
        fwlog log = new fwlog(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\kcadlog.txt", "INFOERRODEBUG");
        private static System.Timers.Timer timer = new System.Timers.Timer();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                reconstroi();

                timer.Interval = (double)10000;
                timer.Enabled = true;
                //Assign a new event handler to the Elapsed method
                timer.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
                timer.Start();
                log.info("Serviço iniciado", "", "");
            }
            catch (Exception ex)
            {
                log.erro("ao iniciar o serviço", "", ex.Message.ToString() + "|" + ex.StackTrace);
            }
        }

        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            // Stop the Timer
            timer.Stop();
            timer.Enabled = false;

            // Execute the function
            rodarVerificacao();

            // Enable the timer
            timer.Start();
            timer.Enabled = true;
        }

        protected override void OnStop()
        {
            try
            {
                log.info("Serviço finalizado", "", "");
                timer.Stop();
            }
            catch (Exception ex)
            {
                log.erro("ao stopar o serviço", "", ex.Message.ToString() + "|" + ex.StackTrace);
            }
        }

        private void reconstroi()
        {
            try
            {
                log.info(" inicio reconstroi ", "", "");
                controle ctrl = new controle();
                ctrl.rearranjaArquivos();
                ctrl = null;
                mpcontrole mctrl = new mpcontrole();
                mctrl.rearranjaArquivos();
                mctrl = null;
                log.info(" fim reconstroi ", "", "");
            }
            catch (Exception ex)
            {
                log.erro("ao rodar Reconstroi servico onstart", "", ex.Message.ToString() + "|" + ex.StackTrace);
            }
        }

        private void rodarVerificacao()
        {
#if DEBUG
            //System.Diagnostics.Debugger.Launch();
#endif
            try
            {
                configSRV csrv = new configSRV();
                log.info(" inicio rodarverificacao : timerativo = " + csrv.TIMERATIVO.ToString() + " intervalo =" + csrv.INTERVALO.ToString(), "", "");
                if (csrv.TIMERATIVO)
                {
                    // consulta dos emails 
                    kserv.controle ctrl = new controle();

                    ctrl.iniciarServicosConsultas();
                    ctrl.iniciarServicosReConsultas();
                    ctrl.iniciarFinalizacaoArquivos();

                    ctrl = null;

                    kserv.mpcontrole mpctrl = new mpcontrole();

                    mpctrl.iniciarServicosConsultas();
                    mpctrl.iniciarServicosReConsultas();
                    mpctrl.iniciarFinalizacaoArquivos();

                    mpctrl = null;


                    timer.Interval = (double)csrv.INTERVALO;
                    //nfe = null;
                }
                log.info(" Fim rodarverificacao", "", "");
                csrv = null;
            }
            catch (Exception ex)
            {
                log.erro("ao rodar Verificacao", "", ex.Message.ToString() + "|" + ex.StackTrace);
            }

        }

    }
}
